{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook is intended to demonstrate how vessel segmentation methods of ITKTubeTK can be applied to multi-channel MRI (MRA + T1, T2, etc)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itk\n",
    "from itk import TubeTK as ttk\n",
    "\n",
    "from itkwidgets import view\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "ImageType = itk.Image[itk.F, 3]\n",
    "\n",
    "im1iso = itk.imread(\"MRA-VesselEnhanced.mha\")\n",
    "im1BrainVess = itk.imread(\"MRA-Brain-VesselEnhanced.mha\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "imMath = ttk.ImageMath.New(im1BrainVess)\n",
    "imMath.MedianFilter(1)\n",
    "imMath.Threshold(0.0000000001, 1, 1, 0)\n",
    "im1VessMask = imMath.GetOutputShort()\n",
    "\n",
    "ccSeg = ttk.SegmentConnectedComponents.New(im1VessMask)\n",
    "ccSeg.SetMinimumVolume(4)\n",
    "ccSeg.Update()\n",
    "im1VessMaskCC = ccSeg.GetOutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "59ef94f7946b4352818e3debe2f4fea9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF3; pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "imMathSS = ttk.ImageMath.New(im1VessMaskCC)\n",
    "imMath.Threshold(1,1,0,1)\n",
    "im1VessMaskInv = imMath.GetOutputFloat()\n",
    "distFilter = itk.DanielssonDistanceMapImageFilter.New(im1VessMaskInv)\n",
    "distFilter.Update()\n",
    "im1SeedRadius = distFilter.GetOutput()\n",
    "\n",
    "view(im1SeedRadius)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "numSeeds = 20\n",
    "\n",
    "vSeg = ttk.SegmentTubes.New(Input=im1iso)\n",
    "#vSeg.SetVerbose(True)\n",
    "vSeg.SetMinCurvature(0.0)\n",
    "vSeg.SetMinRoundness(0.0)\n",
    "vSeg.SetMinRidgeness(0.8)\n",
    "vSeg.SetMinLevelness(0.0)\n",
    "vSeg.SetRadiusInObjectSpace( 0.75 )\n",
    "vSeg.SetBorderInIndexSpace(3)\n",
    "vSeg.SetSeedMask( im1SeedRadius )\n",
    "vSeg.SetSeedRadiusMask( im1SeedRadius )\n",
    "vSeg.SetOptimizeRadius(False)\n",
    "vSeg.SetUseSeedMaskAsProbabilities(True)\n",
    "vSeg.SetSeedExtractionMinimumProbability(0.4)\n",
    "#vSeg.SetSeedMaskMaximumNumberOfPoints( numSeeds )\n",
    "vSeg.ProcessSeeds()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8f3a8fbef7e6440fb0f170ba5b27602b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF3; pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tubeMaskImage = vSeg.GetTubeMaskImage()\n",
    "view(tubeMaskImage)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "SOWriter = itk.SpatialObjectWriter[3].New(vSeg.GetTubeGroup())\n",
    "SOWriter.SetFileName( \"MRA-Vessels.tre\" )\n",
    "SOWriter.Update()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
